home *** CD-ROM | disk | FTP | other *** search
- ############################################################################
- #
- # Name: permute.icn
- #
- # Title: Permutations, combinations, and such
- #
- # Author: Ralph E. Griswold and Kurt A. Welgehausen
- #
- # Date: May 9, 1989
- #
- ############################################################################
- #
- # These procedures produce various rearrangements of strings of
- # characters:
- #
- # comb(s,i) generates the combinations characters from s taken
- # i at a time.
- #
- # permute(s) generates all the permutations of the string s.
- #
- # menader(s,n) produces a "meandering" string which contains all
- # n-tuples of characters of s.
- #
- # csort(s) produces the characters of s in lexical order.
- #
- # ochars(s) produces the unique characters of s in the order they
- # first appear in s.
- #
- # schars(s) produces the unique characters of s in lexical order.
- #
- ############################################################################
-
- procedure comb(s,i)
- local j
-
- if i < 1 then fail
- suspend if i = 1 then !s
- else s[j := 1 to *s - i + 1] || comb(s[j + 1:0],i - 1)
- end
-
- procedure permute(s)
- local i
-
- if *s = 0 then return ""
- suspend s[i := 1 to *s] || permute(s[1:i] || s[i+1:0])
- end
-
- procedure meander(alpha,n)
- local result, t, i, c, k
-
- i := k := *alpha
- t := n - 1
- result := repl(alpha[1],t)
- while c := alpha[i] do {
- if find(result[-t:0] || c,result)
- then i -:= 1
- else {
- result ||:= c
- i := k
- }
- }
- return result
- end
-
- procedure csort(s)
- local c, s1
-
- s1 := ""
- every c := !cset(s) do
- every find(c,s) do
- s1 ||:= c
- return s1
- end
-
- procedure schars(s)
- return string(cset(s))
- end
-
- procedure ochars(w)
- local out, c
-
- out := ""
- every c := !w do
- if not find(c,out) then
- out ||:= c
- return out
- end
-